package GameSrc;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class GameWindow extends JFrame {
    private final int WIDTH =1024;

    private final int HEIGHT = 612;

    private final int canX=550;

    private final int canY=500;

    private int status = 0;


    private int integral=0;

    private double tan = 0;

    private boolean sure=false;

    Cannon cannon;

    GamePaint gamePaint = new GamePaint(this);

    JButton start = new JButton(new ImageIcon("Game/img/start.png"));

    JButton end = new JButton(new ImageIcon("Game/img/back.png"));

    Random random = new Random();


    ArrayList<Net> nets = new ArrayList<>();

    ArrayList<Fish> fish = new ArrayList<>();

    ArrayList<Net> removeNet=new ArrayList<>();

    ArrayList<Fish> removeFish = new ArrayList<>();


    public GameWindow() throws HeadlessException {
        window();
    }

    public void window(){
        this.setSize(WIDTH,HEIGHT);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setTitle("捕鱼达人");
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        gamePaint.addMouseMotionListener(mouseAdapter);
        gamePaint.addMouseListener(mouseClick);
        refresh();
        gameThread_2(this);
        this.setVisible(true);
    }

    public void gameThread(){
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                attack();
                nets.removeAll(removeNet);
                fish.removeAll(removeFish);
                refresh();
            }
        },0,20);
    }

    public void gameThread_2(GameWindow g){
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (status==1){
                    fish.add(new Fish(1050,random.nextInt(380),g));
                }

            }
        },1000,1000);
    }

    public double getTan() {
        return tan;
    }



    public void gameMenu(){
        JPanel bg = new JPanel();
        bg.setLayout(null);
        bg.setBounds(0,0,WIDTH,HEIGHT);


        ImageIcon title = new ImageIcon("Game/img/title.png");
        JLabel titleLabel = new JLabel(title);
        titleLabel.setBounds(250,50,506,288);
        bg.add(titleLabel);


        start.setBounds(400,380,224,96);
        start.addMouseListener(mouseAdapter);
        bg.add(start);


        end.setBounds(420,500,185,60);
        end.addMouseListener(mouseAdapter);
        bg.add(end);

        JLabel bgiLabel = new JLabel(new ImageIcon("Game/img/bgi1.png"));
        bgiLabel.setBounds(0,0,WIDTH,HEIGHT);
        bg.add(bgiLabel);

        this.add(bg);
   }

   public void refresh(){
       if (this.getContentPane().getComponentCount() > 0) {
           this.getContentPane().removeAll();
       }
        if (status==0){
            gameMenu();
        } else if (status == 1) {
            gamePaint.setBounds(0, 0, WIDTH, HEIGHT); // 设置GamePaint面板的大小和位置
            this.add(gamePaint);
        } else if (status==-1){
            System.exit(0);
        }
       this.getContentPane().revalidate();
       this.getContentPane().repaint();
   }

   public void addScare( Fish fish){
        this.integral+=fish.getScare();
   }

   MouseAdapter mouseClick = new MouseAdapter() {
       @Override
       public void mouseClicked(MouseEvent e) {
           super.mouseClicked(e);
           if (status == 1&&e.getButton()==MouseEvent.BUTTON1){
               sure=true;
               System.out.println(sure);
           }
       }
   };


    MouseAdapter mouseAdapter = new MouseAdapter() {
       @Override
       public void mouseClicked(MouseEvent e) {
           super.mouseClicked(e);
           if (e.getSource()==start){
               status = 1;
               gameThread();
           } else if (e.getSource()==end) {
               status=-1;
               refresh();
           }
           if (status==1&&e.getButton()==MouseEvent.BUTTON1&&cannon!=null){
//               cannon.attack();
               System.out.println(1);
           }
       }

       @Override
       public void mouseMoved(MouseEvent e) {
           super.mouseMoved(e);
           int x = e.getX();
           int y = e.getY();
           tan =- Math.atan2 ((canX+52)-x,(canY+100)-y);
       }
   };

    public void attack(){
        if (this.sure){
            nets.add(new Net(590,500,this));
            this.sure=false;
        }
    }


    static class GamePaint extends JPanel {
        GameWindow gameWindow;
        Image offScreenImage; // 双缓冲用的内存图片

        public GamePaint(GameWindow gameWindow) {
            this.gameWindow = gameWindow;
        }

        // 重写update方法实现双缓冲
        @Override
        public void update(Graphics g) {
            if (offScreenImage == null) {
                offScreenImage = createImage(gameWindow.WIDTH, gameWindow.HEIGHT);
            }
            Graphics gOffScreen = offScreenImage.getGraphics();
            paint(gOffScreen);
            g.drawImage(offScreenImage, 0, 0, this);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Image bg;
            try {
                bg = ImageIO.read(new File("Game/img/bgi1.png"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            g.drawImage(bg, 0, 0, this);

            Fort fort = new Fort(0, 370, gameWindow, gameWindow.integral);
            fort.paintFort(g);

            if (!gameWindow.fish.isEmpty()){
                for (int i = 0; i < gameWindow.fish.size(); i++) {
                    gameWindow.fish.get(i).fishMove();
                    gameWindow.fish.get(i).paintFish(g);
                }
            }

            if(!gameWindow.nets.isEmpty()){
                for (int i = 0; i < gameWindow.nets.size(); i++) {
                    gameWindow.nets.get(i).isOutOfBounds();
                    gameWindow.nets.get(i).isCatch();
                    gameWindow.nets.get(i).move();
                    gameWindow.nets.get(i).paintNet(g);
//                    System.out.println();
                }
            }

            Cannon cannon = new Cannon(550, 430, gameWindow, gameWindow.getTan());
            gameWindow.cannon=cannon;
            cannon.paintCannon(g);

        }
    }
}
